package pdd;

import java.util.Arrays;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int len = n*m;
        int[] comeArray = new int[len];
        int[] hArray = new int[len];
        int index = 0;
        while (index < len) {
            comeArray[index] = index;
            hArray[index++] = sc.nextInt();
        }
        reverseArray(comeArray);
        reverseArray(hArray);
        sortArray(comeArray, hArray);

        index = 0;
        int result = 0;
        while(index < len) {
            int leftIndex = index;
            int rightIndex = index + m-1;
            index += m;
            for (int i=rightIndex; i>leftIndex; i--) {
                for (int j=i; j>=leftIndex; j--) {
                    if (comeArray[i] > comeArray[j]) {
                        result++;
                    }
                }
            }
        }
        System.out.println(result);

    }

    private static void sortArray(int[] comeArray, int[] hArray) {
        mergeSortTwoArray(comeArray, hArray, 0, hArray.length-1);
    }

    private static void mergeSortTwoArray(int[] comeArray, int[] hArray, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = left + (right-left)/2;
        mergeSortTwoArray(comeArray, hArray, left, mid);
        mergeSortTwoArray(comeArray, hArray, mid+1, right);

        mergeArray(comeArray, hArray, left, mid, mid+1, right);
    }

    private static void mergeArray(int[] comeArray, int[] hArray, int left, int mid, int midPlus, int right) {
        int len = comeArray.length;
        int[] newComeArray = Arrays.copyOf(comeArray, len);
        int[] newHArray = Arrays.copyOf(hArray, len);

        int leftIndex = left;
        int rightIndex = midPlus;
        int nowIndex = left;
        while (leftIndex <= mid || rightIndex <= right) {
            if (leftIndex > mid) {  // 左边已经走完了
                System.arraycopy(hArray, rightIndex, newHArray, nowIndex, right-rightIndex+1);
                System.arraycopy(comeArray, rightIndex, newComeArray, nowIndex, right-rightIndex+1);
                break;
            }
            if (rightIndex > right) {
                System.arraycopy(hArray, leftIndex, newHArray, nowIndex, mid-leftIndex+1);
                System.arraycopy(comeArray, leftIndex, newComeArray, nowIndex, mid-leftIndex+1);
                break;
            }
            if (hArray[leftIndex] <= hArray[rightIndex]) {
                newHArray[nowIndex] = hArray[leftIndex];
                newComeArray[nowIndex] = comeArray[leftIndex];
                nowIndex++;
                leftIndex++;
            } else {
                newHArray[nowIndex] = hArray[rightIndex];
                newComeArray[nowIndex] = comeArray[rightIndex];
                nowIndex++;
                rightIndex++;
            }
        }
        System.arraycopy(newHArray, left, hArray, left, right-left+1);
        System.arraycopy(newComeArray, left, comeArray, left, right-left+1);
    }

    private static void reverseArray(int[] array) {
        int len = array.length;

        int[] newArray = new int[len];
        System.arraycopy(array, 0, newArray, 0, len);

        for (int i = 0; i < len; i++) {
            array[i] = newArray[len-i-1];
        }
    }
}
